define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ad/picture/index',
                    add_url: 'ad/picture/add',
                    edit_url: 'ad/picture/edit',
                    del_url: 'ad/picture/del',
                    multi_url: 'ad/picture/multi',
                    table: 'ad',
                }
            });

            var ad_types = JSON.parse($(':hidden[name=ad_types]').val());
            var cats = JSON.parse($(':hidden[name=cats]').val());
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'picture', title: __('Picture'), operate: false,formatter: Table.api.formatter.image},
                        {field: 'width', title: __('Width'), operate: false},
                        {field: 'height', title: __('Height'), operate: false},
                        {field: 'type_id', title: __('Type_id'), formatter: Table.api.formatter.status, searchList: ad_types, style: 'min-width:100px;'},
                        {field: 'cat_id', title: __('Cat_id'), formatter: Table.api.formatter.status, searchList: cats, style: 'min-width:100px;'},
                        {field: 'ad_name', title: __('Ad_name'), operate: false},
                        {field: 'summary', title: __('Summary'), operate: false, style: 'width:200px;'},
                        {field: 'link', title: __('Link'), operate: false},
                        {field: 'start_time', title: __('Start_time'), operate: false, formatter: Table.api.formatter.date},
                        {field: 'end_time', title: __('End_time'), operate: false, formatter: Table.api.formatter.date},
                        {field: 'sort', title: __('Sort'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate: false, formatter: Table.api.formatter.date},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                commonSearch: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});